﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using WebUI.Areas.Bots.Models;
using DAL;
using Services;
using Entities;

namespace WebUI.Areas.Bots.Controllers
{
    public partial class NotificationsController : BaseController
    {
        #region Member Variables

        readonly IBotRecipientRepository _botRecipientRepository;
        readonly IBotService _botService;

        #endregion

        #region Constructor

        public NotificationsController(ISessionHelper sessionHelper, IBotRecipientRepository botRecipientRepository, IBotService botService)
            : base(sessionHelper)
        {
            this._botRecipientRepository = botRecipientRepository;
            this._botService = botService;
        }

        #endregion

        #region Action Methods

        public virtual ViewResult Index(NotificationSetting notificationSetting)
        {
            notificationSetting.NotificationType = _sessionHelper.Bot.BotNotificationType;
            notificationSetting.Recepients = _sessionHelper.Bot.BotRecipients;

            return View(notificationSetting);
        }

        [HttpPost]
        public virtual PartialViewResult AddRecepient(NotificationSetting notificationSetting, BotRecipient botRecipient)
        {
            if (ModelState.IsValid)
            {
                var recepient = notificationSetting.Recepients.SingleOrDefault(r => r.RecipientName == botRecipient.RecipientName && r.RecipientEmail == botRecipient.RecipientEmail);

                if (recepient == null)
                {
                    recepient = new BotRecipient { RecipientName = botRecipient.RecipientName, RecipientEmail = botRecipient.RecipientEmail };
                    notificationSetting.Recepients.Add(recepient);
                }

                notificationSetting.BotRecipient = new BotRecipient();
            }

            return PartialView(MVC.Bots.Notifications.Views._recepients, notificationSetting.Recepients);
        }

        [HttpPost]
        public virtual PartialViewResult RemoveRecepient(NotificationSetting notificationSetting, BotRecipient botRecipient)
        {
            var recepient = notificationSetting.Recepients.SingleOrDefault(n => n.RecipientEmail == botRecipient.RecipientEmail && n.RecipientName == botRecipient.RecipientName);
            notificationSetting.Recepients.Remove(recepient);

            return PartialView(MVC.Bots.Notifications.Views._recepients, notificationSetting.Recepients);
        }

        [HttpPost]
        public virtual ActionResult Save(NotificationSetting notificationSetting, string NotificationType)
        {
            _sessionHelper.Bot.BotNotificationType = short.Parse(NotificationType);

            IDictionary<string, string> validationDictionary = _botService.ValidateRecepientsStep(_sessionHelper.Bot);

            foreach (var entry in validationDictionary.Values)
            {
                ModelState.AddModelError(string.Empty, entry);
            }

            if (ModelState.IsValid)
            {
                _botService.SaveBot(_sessionHelper.Bot);
                return RedirectToAction(MVC.Bots.Index.Index());
            }

            return View(MVC.Bots.Notifications.Views.Index, notificationSetting);
        }

        #endregion
    }
}